import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import router from './router'
import { getToken } from './utils/auth'
import store from './store'
const whiteList = ['/404', '/home', '/login', '/modul3', '/modul1', '/modul2'] // no redirect whitelist
NProgress.configure({
  showSpinner: false
})
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const hasToken = getToken()
  const hasGetUserInfo = store.getters.name
  if (hasToken) {
    if (hasGetUserInfo) {
      next()
    } else {
      await store.dispatch('getInfo')
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next(`/404`)
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
